package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.redp.bizimpl.igas.domain.PBinInfo;
import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.utils.DateUtil;
import cc.rengu.redp.common.utils.SortUtil;
import cc.rengu.redp.bizimpl.upmp.dao.WctFstInfoMapper;
import cc.rengu.redp.bizimpl.upmp.domain.WctFstInfo;
import cc.rengu.redp.bizimpl.upmp.service.WctFstInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author LiuYuLiang.LL
 */
@Service
public class WctFstInfoServiceImpl extends ServiceImpl<WctFstInfoMapper, WctFstInfo> implements WctFstInfoService {
    @Autowired
    private IUpmpSysParamService upmpSysParamService;
    @Override
    public IPage<WctFstInfo> findByPage(QueryRequest request, WctFstInfo info) {
        try {
            LambdaQueryWrapper<WctFstInfo> queryWrapper = new LambdaQueryWrapper<>();
            if (StringUtils.isNotBlank(info.getWctFstNo())) {
                queryWrapper.like(WctFstInfo::getWctFstNo, info.getWctFstNo());
            }
            if (StringUtils.isNotBlank(info.getWctFstName())) {
                queryWrapper.like(WctFstInfo::getWctFstName, info.getWctFstName());
            }

            queryWrapper.orderByDesc(WctFstInfo::getCreateTime);
            Page<WctFstInfo> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            return this.page(page, queryWrapper);
        } catch (Exception e) {
            log.error("查询失败", e);
            return null;
        }
    }
    @Override
    @Transactional
    public void create(WctFstInfo entity) {
        // 获取当前登录用户信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String lastUpddt = DateUtil.getDateFormat(new Date(),DateUtil.FULL_TIME_PATTERN);
        entity.setStatus(BusinessContainer.data_sta_01);
        entity.setAuditStatus(BusinessContainer.status_02);
        entity.setOprId(user.getId());
        entity.setCrtOrgId(user.getOrgCode());
        entity.setCreateTime(lastUpddt);
        entity.setLastOprId(user.getId());
        entity.setLastUpdateTime(lastUpddt);
        this.baseMapper.insert(entity);
    }
    @Override
    @Transactional
    public void update(WctFstInfo entity) {
        // 获取当前登录用户信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String lastUpddt = DateUtil.getDateFormat(new Date(),DateUtil.FULL_TIME_PATTERN);
        // 修改
        entity.setAuditStatus(BusinessContainer.status_42);
        entity.setLastOprId(user.getId());
        entity.setLastUpdateTime(lastUpddt);
        this.baseMapper.updateById(entity);
    }
    @Override
    @Transactional
    public void cancel(String id) {
        // 注销
        WctFstInfo entity = this.getById(id);
        entity.setStatus(BusinessContainer.data_sta_03);
        entity.setAuditStatus(BusinessContainer.status_12);
        this.baseMapper.updateById(entity);
    }

    @Override
    @Transactional
    public void delete(String[] ids) {
        List<String> list = Arrays.asList(ids);
        this.baseMapper.deleteBatchIds(list);
    }

    /**
     * 是否存在二级类目
     * @param wctFstNo 微信商户一级类目编码
     * @return
     */
    @Override
    public String isExistSubDirect(String wctFstNo) {
        List<String> result = this.baseMapper.isExistSubDirect(wctFstNo);
        if (result == null || result.size() == 0) {
            return "N";
        } else {
            return "Y";
        }
    }

    /**
     * 查询所有正常状态微信一级类目代码
     * @return
     */
    @Override
    public List<String> findAllWctFstNo() {
        return this.baseMapper.findAllWctFstNo();
    }

    @Override
    public Map<String, Object> wctfstSelect2List(String q, QueryRequest request) {

        if (null == q) {
            return null;
        }
        q = q.replaceAll("&amp;", "&");

        String issuerCodeOrName = "";
        String[] param = q.split("&");
        if (null != param && param.length > 0) {
            issuerCodeOrName = param[0];
        }

        try {
            QueryWrapper<WctFstInfo> queryWrapper = new QueryWrapper<>();

            if (StringUtils.isNotEmpty(issuerCodeOrName)) {
                String finalIssuerCodeOrName = issuerCodeOrName;
                queryWrapper.and(qw -> qw.lambda().likeRight(WctFstInfo::getWctFstNo, finalIssuerCodeOrName).or().likeRight(WctFstInfo::getWctFstName, finalIssuerCodeOrName));
            }

            Page<WctFstInfo> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            Page<WctFstInfo> page1 = this.page(page, queryWrapper);
            List<WctFstInfo> listx = page1.getRecords();

            Map<String, Object> maps = new HashMap<String, Object>();
            Map<String, String> map = new HashMap<String, String>();
            List<Object> list = new ArrayList<Object>();
            if (listx != null) {
                for (int i = 0; i < listx.size(); i++) {
                    WctFstInfo org = listx.get(i);
                    map = new HashMap<String, String>();
                    map.put("id", org.getWctFstNo());
                    map.put("name", org.getWctFstName());

                    list.add(map);
                }
                maps.put("results", list);
                maps.put("total", page.getTotal());
                int pageIndex = Common.isBlank(request.getPageNum()) ? 1 : request.getPageNum();
                if (pageIndex == page1.getTotal()) {
                    maps.put("more", false);
                } else {
                    maps.put("more", true);
                }
            }
            return maps;

        } catch (Exception e) {
            log.error("查询上级类目代码出现异常", e);
            return null;
        }
    }

    /**
     * 根据条件查询所有正常状态微信一级类目代码
     * @param wctFstNo 微信一级类目代码
     * @return
     */
    public List<String> findwctFstNo(String wctFstNo) {
        return this.baseMapper.findwctFstNo(wctFstNo);
    }

    public List<String> findwctFstName(String wctFstName) {
        return this.baseMapper.findwctFstName(wctFstName);
    }

}
